#include "process.h"

#define NUM 101
#define STYLE '#'

//进度条version2 -- 传入总大小和已下载的大小，通过比例打印进度条
void FlushProcess(double total, double current)
{
	//初始化进度条
	char buffer[NUM];
	memset(buffer, 0, sizeof(buffer));
	const char* label = "|/-\\";
	int len = strlen(label);

	static int cnt = 0;

	//根据比例填充进度条
	int num = (int)(current*100 / total);
	for(int i = 0; i < num; i++)
	{
		buffer[i] = STYLE;
	}
	double rate = current / total;
	cnt %= len;
	printf("[%-100s][%.1f%%][%c]\r", buffer, rate*100, label[cnt]);
	cnt++;
	fflush(stdout);
}

//进度条版本1 -- 这个版本无法套用到下载或上传中，其中进度条的执行是写死的，进度条执行完的时间是固定的
void process_v1()
{
	char buffer[NUM];
	memset(buffer, 0, sizeof(buffer));
	const char* label = "|/-\\";
	int len = strlen(label);

	int cnt = 0;
	while(cnt <= 100)
	{
		printf("[%-100s][%d%%][%c]\r", buffer, cnt, label[cnt % len]);
		fflush(stdout);
		buffer[cnt] = STYLE;
		cnt++;
		usleep(50000);
	}
	printf("\n");
}
